package cn.net.autocode.platform.datasource;

import cn.net.autocode.core.utils.TreeUtils;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.dbManager.SqlWrapper;
import cn.net.autocode.dbManager.SqlWrapperBuilder;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@AutoCodeQuery(value = "platformMenu", description = "组织机构菜单授权")
public class PlatFormMenuDataSource {
    private final Dao dao;

    public PlatFormMenuDataSource(Dao dao) {
        this.dao = dao;
    }



    @AutoCodeQuery(value = "authOrgan", description = "查询该菜单没有授权的组织机构")
    public JSONObject authOrgan(JSONObject param){
        JSONObject pagingData = param.getJSONObject("pagingData");
        JSONObject operData = param.getJSONObject("operData");
        String menuId = operData.getString("menuId");
        if(menuId == null || menuId.isBlank()){
            throw new RuntimeException("menuId is null");
        }

        String menuType = operData.getString("menuType");
        if(menuType == null || menuType.isBlank()){
            menuType = "PC";
        }

        SqlWrapperBuilder swb;

        if("APP".equals(menuType)){
            swb = new SqlWrapperBuilder("sys_organ t1,app_organ_menu t2");
        }else{
            swb = new SqlWrapperBuilder("sys_organ t1,sys_organ_menu t2");
        }

        swb.where("t1.code=t2.org_code and t2.menu_id=?", menuId);

        String keyWord = operData.getString("keyWord");
        if(keyWord != null && !keyWord.isBlank()){
            String likeKey = "%"+keyWord+"%";
            swb.where("(t1.code like ? or t1.label like ?)", likeKey, likeKey);
        }

        SqlWrapper countSqlWrapper = swb.countSqlWrapper();
        SqlWrapper sqlWrapper = swb.sqlWrapper();

        dao.pageInfo(pagingData,countSqlWrapper.getSql(),countSqlWrapper.getParams().toArray());

        JSONArray data = dao.queryJSONArrayByPage(sqlWrapper.getSql(),pagingData,sqlWrapper.getParams().toArray());

        JSONObject result = new JSONObject();
        result.put("data", data);
        result.put("pagingData", pagingData);
        return result;
    }


    @AutoCodeQuery(value = "notAuthOrgan", description = "查询该菜单没有授权的组织机构")
    public JSONObject notAuthOrgan(JSONObject param){
        JSONObject pagingData = param.getJSONObject("pagingData");
        JSONObject operData = param.getJSONObject("operData");
        String menuId = operData.getString("menuId");
        String menuType = operData.getString("menuType");
        if(menuType == null || menuType.isBlank()){
            menuType = "PC";
        }
        if(menuId == null || menuId.isBlank()){
            throw new RuntimeException("menuId is null");
        }

        SqlWrapperBuilder swb = new SqlWrapperBuilder("sys_organ t1");
        if("APP".equals(menuType)){
            swb.where("not exists(select 1 from app_organ_menu t2 where t1.code=t2.org_code and t2.menu_id=?)", menuId);
        }else{
            swb.where("not exists(select 1 from sys_organ_menu t2 where t1.code=t2.org_code and t2.menu_id=?)", menuId);
        }

        String keyWord = operData.getString("keyWord");
        if(keyWord != null && !keyWord.isBlank()){
            String likeKey = "%"+keyWord+"%";
            swb.where("(t1.code like ? or t1.label like ?)", likeKey, likeKey);
        }

        SqlWrapper countSqlWrapper = swb.countSqlWrapper();
        SqlWrapper sqlWrapper = swb.sqlWrapper();

        dao.pageInfo(pagingData,countSqlWrapper.getSql(),countSqlWrapper.getParams().toArray());

        JSONArray data = dao.queryJSONArrayByPage(sqlWrapper.getSql(),pagingData,sqlWrapper.getParams().toArray());

        JSONObject result = new JSONObject();
        result.put("data", data);
        result.put("pagingData", pagingData);
        return result;
    }

    @AutoCodeQuery(value = "organMenusTree", description = "查询组织机构已授权的菜单信息，树形结构显示")
    public JSONObject organMenusTree(JSONObject param){
        String orgCode = param.getString("orgCode");

        if(orgCode == null || orgCode.isBlank()){
            throw new RuntimeException("orgCode is null");
        }
        //获取菜单信息
        String sql1 = "select * from sys_menus order by sort_no";
        JSONArray array = dao.queryJSONArray(sql1);
        JSONArray menuTree = TreeUtils.commonTree(array);

        //获取选中的菜单
        String sql2 = "select menu_id from sys_organ_menu where org_code=?";
        List<String> checkedList1 = dao.queryListValue(sql2,String.class,orgCode);

        String sql3 = "select id from sys_menus where is_public='Y'";
        List<String> checkedList2 = dao.queryListValue(sql3,String.class);
        for(String id : checkedList2){
            if (!checkedList1.contains(id)) {
                checkedList1.add(id);
            }
        }

        String[] defaultCheckedKeys = checkedList1.toArray(new String[0]);

        JSONObject result = new JSONObject();
        result.put("data", menuTree);
        result.put("defaultCheckedKeys", defaultCheckedKeys);
        return result;
    }

    @AutoCodeQuery(value = "appOrganMenusTree", description = "查询组织机构已授权的菜单信息，树形结构显示")
    public JSONObject appOrganMenusTree(JSONObject param){
        String orgCode = param.getString("orgCode");

        if(orgCode == null || orgCode.isBlank()){
            throw new RuntimeException("orgCode is null");
        }
        //获取菜单信息
        String sql1 = "select * from app_menus order by sort_no";
        JSONArray array = dao.queryJSONArray(sql1);
        JSONArray menuTree = TreeUtils.commonTree(array);

        //获取选中的菜单
        String sql2 = "select menu_id from app_organ_menu where org_code=?";
        List<String> checkedList1 = dao.queryListValue(sql2,String.class,orgCode);


        String[] defaultCheckedKeys = checkedList1.toArray(new String[0]);

        JSONObject result = new JSONObject();
        result.put("data", menuTree);
        result.put("defaultCheckedKeys", defaultCheckedKeys);
        return result;
    }
}
